/*
 * @Description: In User Settings Edit
 * @Author: your name
 * @Date: 2019-08-13 20:41:43
 * @LastEditTime: 2019-08-13 21:20:32
 * @LastEditors: Please set LastEditors
 */
const parse = require('url').parse

module.exports = function(obj){
    return function(req,res,next){
        if(!obj[req.method]){
            next()
            return
        }
        var routes = obj[req.method]
        var url = parse(req.url)
        var paths = Object.keys(routes)

        for(var i=0;i<paths.length;i++){
            var path = paths[i]
            var fn = routes[path]
            console.log(path,'------')
            path = path.replace(/\//g,'\\/').replace(/:(\w+)/g,'([^\\/]+)')
            console.log(path)

            var re = new RegExp('^'+path+'$')
            var captures = url.pathname.match(re)
            console.log(url.pathname,'+++')
            if(captures){
                var args = [req,res].concat(captures.slice(1))
                fn.apply(null,args)
                return
            }
        }
        next()
    }
}